class_name UIManager extends Node

# UI状态
enum UIState {
	MAIN_MENU,      # 主菜单
	WORLD_MAP,      # 世界地图
	BATTLE,         # 战斗界面
	INVENTORY,      # 背包界面
	MONSTER_INFO,   # 幻兽信息
	EVOLUTION,      # 进化/合成界面
	SHOP,           # 商店界面
	DIALOGUE,       # 对话界面
	SETTINGS        # 设置界面
}

# 当前UI状态
var current_state = UIState.MAIN_MENU

# UI元素引用
var ui_elements = {}

# 信号
signal ui_state_changed(new_state, old_state)
signal ui_element_clicked(element_name)

# 初始化UI管理器
func initialize():
	print("初始化UI管理器")
	return self

# 切换UI状态
func change_state(new_state):
	var old_state = current_state
	current_state = new_state
	
	print("UI状态从 %s 切换到 %s" % [get_state_name(old_state), get_state_name(new_state)])
	emit_signal("ui_state_changed", new_state, old_state)
	
	# 根据新状态更新UI显示
	_update_ui_for_state(new_state)
	
	return true

# 根据状态更新UI显示
func _update_ui_for_state(state):
	# 隐藏所有UI元素
	for element_name in ui_elements:
		if ui_elements[element_name] != null:
			ui_elements[element_name].visible = false
	
	# 根据状态显示相应UI元素
	match state:
		UIState.MAIN_MENU:
			_show_ui_element("main_menu")
		
		UIState.WORLD_MAP:
			_show_ui_element("world_map")
			_show_ui_element("player_status")
			_show_ui_element("mini_menu")
		
		UIState.BATTLE:
			_hide_ui_element("main_menu")
			_show_ui_element("battle_scene")
			_show_ui_element("player_status")
			_show_ui_element("enemy_status")
			_show_ui_element("battle_ui")
			_show_ui_element("monster_status")
		
		UIState.INVENTORY:
			_show_ui_element("inventory_panel")
			_show_ui_element("player_status")
		
		UIState.MONSTER_INFO:
			_show_ui_element("monster_info_panel")
			_show_ui_element("player_status")
		
		UIState.EVOLUTION:
			_show_ui_element("evolution_panel")
			_show_ui_element("player_status")
		
		UIState.SHOP:
			_show_ui_element("shop_panel")
			_show_ui_element("player_status")
			_show_ui_element("inventory_panel")
		
		UIState.DIALOGUE:
			_show_ui_element("dialogue_panel")
		
		UIState.SETTINGS:
			_show_ui_element("settings_panel")

# 显示指定UI元素
func _show_ui_element(element_name):
	if ui_elements.has(element_name) and ui_elements[element_name] != null:
		ui_elements[element_name].visible = true
	else:
		print("警告: UI元素 '%s' 不存在或为空" % element_name)

# 隐藏指定UI元素
func _hide_ui_element(element_name):
	if ui_elements.has(element_name) and ui_elements[element_name] != null:
		ui_elements[element_name].visible = false
	else:
		print("警告: UI元素 '%s' 不存在或为空" % element_name)

# 注册UI元素
func register_ui_element(element_name, element_node):
	ui_elements[element_name] = element_node
	print("注册UI元素: %s" % element_name)
	return true

# 获取UI元素
func get_ui_element(element_name):
	if ui_elements.has(element_name):
		return ui_elements[element_name]
	else:
		print("警告: UI元素 '%s' 不存在" % element_name)
		return null

# 处理UI元素点击
func handle_element_click(element_name):
	print("UI元素被点击: %s" % element_name)
	emit_signal("ui_element_clicked", element_name)
	
	# 根据点击的元素执行相应操作
	match element_name:
		"inventory_button":
			change_state(UIState.INVENTORY)
		
		"monster_info_button":
			change_state(UIState.MONSTER_INFO)
		
		"evolution_button":
			change_state(UIState.EVOLUTION)
		
		"settings_button":
			change_state(UIState.SETTINGS)
		
		"back_button":
			_handle_back_button()
	
	return true

# 处理返回按钮
func _handle_back_button():
	match current_state:
		UIState.INVENTORY, UIState.MONSTER_INFO, UIState.EVOLUTION, UIState.SETTINGS:
			change_state(UIState.WORLD_MAP)
		
		UIState.SHOP:
			change_state(UIState.WORLD_MAP)
		
		UIState.DIALOGUE:
			change_state(UIState.WORLD_MAP)
		
		_:
			print("警告: 当前状态无法返回")

# 更新战斗UI
func update_battle_ui(player_monsters, enemy_monsters, current_turn):
	var battle_ui = get_ui_element("battle_ui")
	if battle_ui != null:
		# 这里将来会实现更新战斗UI的逻辑
		print("更新战斗UI")
	else:
		print("警告: 战斗UI不存在")

# 更新幻兽状态UI
func update_monster_status_ui(monster):
	var monster_status = get_ui_element("monster_status")
	if monster_status != null:
		# 这里将来会实现更新幻兽状态UI的逻辑
		print("更新幻兽状态UI: %s (Lv.%d)" % [monster.monster_name, monster.level])
	else:
		print("警告: 幻兽状态UI不存在")

# 更新玩家状态UI
func update_player_status_ui(player):
	var player_status = get_ui_element("player_status")
	if player_status != null:
		# 这里将来会实现更新玩家状态UI的逻辑
		print("更新玩家状态UI: %s (Lv.%d)" % [player.player_name, player.player_level])
	else:
		print("警告: 玩家状态UI不存在")

# 显示对话框
func show_dialogue(speaker_name, dialogue_text, options = null):
	change_state(UIState.DIALOGUE)
	
	var dialogue_panel = get_ui_element("dialogue_panel")
	if dialogue_panel != null:
		# 这里将来会实现显示对话的逻辑
		print("显示对话: %s - \"%s\"" % [speaker_name, dialogue_text])
		
		# 如果有选项，显示选项按钮
		if options != null and options.size() > 0:
			print("对话选项:")
			for i in range(options.size()):
				print("  %d. %s" % [i + 1, options[i]])
	else:
		print("警告: 对话面板不存在")

# 显示通知
func show_notification(message, duration = 2.0):
	print("显示通知: %s (持续 %.1f 秒)" % [message, duration])
	# 这里将来会实现显示通知的逻辑
	return true

# 获取状态名称
func get_state_name(state = null):
	if state == null:
		state = current_state
	
	match state:
		UIState.MAIN_MENU:
			return "主菜单"
		UIState.WORLD_MAP:
			return "世界地图"
		UIState.BATTLE:
			return "战斗界面"
		UIState.INVENTORY:
			return "背包界面"
		UIState.MONSTER_INFO:
			return "幻兽信息"
		UIState.EVOLUTION:
			return "进化/合成界面"
		UIState.SHOP:
			return "商店界面"
		UIState.DIALOGUE:
			return "对话界面"
		UIState.SETTINGS:
			return "设置界面"
		_:
			return "未知"
